package lj.courseinterect.rest.resources;

import lj.courseinterect.dao.i.ExamInfoDao;
import lj.courseinterect.dao.i.NoticeInfoDao;
import lj.courseinterect.entity.ExamInfo;
import lj.courseinterect.entity.NoticeInfo;
import lj.courseinterect.transfer.OperationTransfer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Date;
import java.util.List;


@Component
@Path("/{a:tea|stu}/notices")
public class NoticeInfoResource
{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private NoticeInfoDao noticeInfoDao;


    public Logger getLogger() {
        return logger;
    }

    public NoticeInfoDao getNoticeInfoDao() {
        return noticeInfoDao;
    }

    public void setNoticeInfoDao(NoticeInfoDao noticeInfoDao) {
        this.noticeInfoDao = noticeInfoDao;
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public OperationTransfer create(NoticeInfo noticeInfo)
    {
        this.logger.info("create(): " +noticeInfo.getNotTime() );
        OperationTransfer operationTransfer;

        noticeInfo.setNotTime(new Date());
        try {
            this.noticeInfoDao.save(noticeInfo);
            operationTransfer = new OperationTransfer(true, "添加公告信息成功");
        } catch (Exception ex){
            operationTransfer = new OperationTransfer(false, "添加公告信息失败");
        }

        this.logger.info("create(): " + operationTransfer.toString());
        return operationTransfer;
    }

    @GET
    @Path("getFirstNotice/{couId: [0-9]*$}")
    @Produces(MediaType.APPLICATION_JSON)
    public NoticeInfo getFirstNotice(@PathParam("couId") Long couId){
        return noticeInfoDao.findByCouId(couId);
    }

    @GET
    @Path("getAll/{couId: [0-9]*$}")
    @Produces(MediaType.APPLICATION_JSON)
    public List<NoticeInfo> getAll(@PathParam("couId") Long couId){
        return noticeInfoDao.queryByCouId(couId);
    }

    @DELETE
    @Path("{notId}")
    @Produces(MediaType.APPLICATION_JSON)
    public OperationTransfer deleteNotice(@PathParam("notId") Long notId){
        try{
            noticeInfoDao.delete(notId);
            return new OperationTransfer(true, "删除成功");
        } catch (Exception ex){
            return new OperationTransfer(false, "删除失败");
        }
    }

}